package com.kangdi.riggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kangdi.riggie.common.Result;
import com.kangdi.riggie.dto.DishDto;
import com.kangdi.riggie.entity.Category;
import com.kangdi.riggie.entity.Dish;
import com.kangdi.riggie.entity.DishFlavor;
import com.kangdi.riggie.service.CategoryService;
import com.kangdi.riggie.service.DishFlavorService;
import com.kangdi.riggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto){
        dishService.saveWtihOther(dishDto);
        return Result.success("添加成功");
    }
    @GetMapping("page")
    public Result<Page> page(int page,int pageSize,String  name){

        Page<DishDto> dishDtoPage = new Page<>();
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        queryWrapper.eq(name!=null,Dish::getName,name);
        dishService.page(dishPage,queryWrapper);
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();
        List<DishDto> dishDtoList= records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category!=null) {
                dishDto.setCategoryName(category.getName());
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtoList);
        return Result.success(dishDtoPage);
    }
    @GetMapping("{id}")
    public Result<DishDto> get(@PathVariable long id){
        DishDto dishDto = dishService.getDishWhitFlavor(id);
        return Result.success(dishDto);
    }
    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){
        dishService.updateDishWtihFlavor(dishDto);
        return Result.success("更改成功");
    }
    @PostMapping("status/{a}")
    public Result<String> updateStatus0(@PathVariable int a, long[] ids){
        for (long id : ids) {
            Dish dish = new Dish();
            dish.setStatus(a);
            dish.setId(id);
            dishService.updateById(dish);
        }

        return Result.success("修改成功!");
    }

    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return Result.success("删除成功");
    }
  /*  @GetMapping("list")
    public Result<List<Dish>> list(long categoryId){
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);
        return Result.success(list);
    }*/
  @GetMapping("list")
  public Result<List<DishDto>> list(Dish dish){
      LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
      dishLambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
      List<Dish> list = dishService.list(dishLambdaQueryWrapper);
      List<DishDto> dtoList=list.stream().map((item)->{
          DishDto dishDto = new DishDto();
          BeanUtils.copyProperties(item,dishDto);
          Long id = item.getId();
          LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
          queryWrapper.eq(DishFlavor::getDishId,id);
          List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
          dishDto.setFlavors(flavors);
          return dishDto;
      }).collect(Collectors.toList());
      return Result.success(dtoList);
  }
}
